import * as Router from 'koa-router';
import dingTalkApi from 'dingtalk-app/dtalk/DingTalkApi';
import { IRouterContext } from 'koa-router';
import BPM from '../lib/db/BPM';
import authService from '../services/authService';
import * as Url from 'url';
//BPM流程
let router=new Router();
router.prefix('/api/BPM')
//跳转
router.get('/skip',async (ctx: IRouterContext, next: any)=>{
    let rq:any=ctx.request;
    let url_new = Url.parse(rq.url);
    //console.log(url_new,'============origUrlObj===========')
    let guid_:string=url_new.query;
    let guid2_:any='';
    if(guid_){
        if(guid_.includes('GUID=')){
           guid2_=guid_.substr('GUID='.length,guid_.length);
            console.log(guid2_,'????????????')
        }
    }
    let guid2=guid2_?guid2_:'';
    console.log(guid2,'guid2guid2guid2guid2guid2')
    if(guid2){
         //let url3_='http://211.95.6.53:8080/esbpm/LoginHandler.ashx?GUID=ce70f22a9ead41eaace4f2eefce38776&url=wx/wxflow.aspx?type=1';
        let url4_=`http://211.95.6.53:8080/esbpm/LoginHandler.ashx?GUID=${guid2}&url=wx/wxflow.aspx?type=1`;   
        ctx.response.redirect(url4_);
        ctx.response.status=302;
    }else{
        console.log('BPM中没有设置GUID值,不能登陆BPM系统!!!!!')
    }
    ctx.body={}
    await next();
})

/* 

//门户
router.get('/mhName', async (ctx: IRouterContext, next: any) => {
    let data = await BPM.query(
        "select n2mhname as mhname,navigateUrl from v_menhu where navigateUrl like '%flowmodelid=%' and n2mhname <> '长星门户'"
    )
    console.log('========门户=====',data)
    ctx.body = data;
    ctx.status = 200
    await next()
});

//阅知计数
router.post('/CountRead', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let count = await BPM.query({
        query: "select count(*) as c from View_Flow_Read_dd where loginname = ?",
        values: [jobnumber]
    });
    console.log(count,'===阅知计数==count=====count==')
    ctx.body = count;
    ctx.status = 200
    await next()
});

//阅知流程
router.post('/Read', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    //console.log(jobnumber);
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    let data = await BPM.query({
        query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY ReceiveTime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Read_dd where loginname = ?) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
        values: [jobnumber, page1, page2]
    })
    console.log(data,'========read======data======')
    ctx.body = data;
    ctx.status = 200
    await next()
});

//阅知流程搜索    body传输数据
router.post('/ReadSearch', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    if (rq.body.FlowID) {
        console.log(rq.body.FlowID);
        let data = await BPM.query({
            query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY ReceiveTime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Read_dd where loginname = ? and FlowID = ?) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
            values: [jobnumber, rq.body.FlowID, page1, page2]
        })
        console.log(data,'========read======data======')
        ctx.body = data;
    } else {
        console.log(rq.body.Subject);
        let data = await BPM.query({
            query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY ReceiveTime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Read_dd where loginname = ? and Subject like ?) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
            values: [jobnumber, '%' + rq.body.Subject + '%', page1, page2]
        })
        console.log(data,'========read======data======')
        ctx.body = data;
    }
    ctx.status = 200
    await next()
});

//待办流程    body传输数据
router.post('/Undo', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    let data = await BPM.query({
        query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY logintime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Undo_dd where jobnumber = ?) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
        values: [jobnumber, page1, page2]
    })
    console.log(data,'========undo======data======')
    ctx.body = data;
    ctx.status = 200
    await next()
});

//待办流程搜索    body传输数据
router.post('/UndoSearch', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    if (rq.body.FlowID) {
        let data = await BPM.query({
            query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY logintime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Undo_dd where jobnumber = ? and FlowID = ? ) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
            values: [jobnumber,rq.body.FlowID, page1, page2]
        })
        console.log(data,'========undoSearch======data======')
        ctx.body = data;
    } else {
        let data = await BPM.query({
            query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY logintime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Undo_dd where jobnumber = ? and  Subject like ?) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
            values: [jobnumber, '%' + rq.body.Subject + '%', page1, page2]
        })
        console.log(data,'========undoSearch======data======')
        ctx.body = data;
    }
    ctx.status = 200
    await next()
});

//待办计数
router.post('/CountUndo', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let count = await BPM.query({
        query: "select count(*) as c from View_Flow_Undo_dd where jobnumber = ?",
        values: [jobnumber]
    });
    console.log(count,'===待办计数==countUndo=====countUndo==')
    ctx.body = count;
    ctx.status = 200
    await next()
});

//接收流程
router.post('/Recive', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    let data = await BPM.query({
        query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY logintime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Recive_dd where jobnumber = ?) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
        values: [jobnumber, page1, page2]
    })
    console.log(data,'===接收流程==Recive =')
    ctx.body = data;
    ctx.status = 200
    await next()
});

//接收流程按流程号搜索
router.post('/ReciveSearch', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    if (rq.body.FlowID) {
        let data = await BPM.query({
            query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY logintime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Recive_dd where jobnumber = ? and FlowID = ? ) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
            values: [jobnumber, rq.body.FlowID, page1, page2]
        })
        console.log(data,'===接收流程搜索==ReciveSearch =')
        ctx.body = data;
    } else {
        let data = await BPM.query({
            query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY logintime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Recive_dd where jobnumber = ? and Subject like ?) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
            values: [jobnumber, '%' + rq.body.Subject + '%', page1, page2]
        })
        console.log(data,'===接收流程搜索==ReciveSearch =')
        ctx.body = data;
    }
    ctx.status = 200
    await next()
});

//接收计数
router.post('/CountRecive', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let count = await BPM.query({
        query: "select count(*) as c from View_Flow_Recive_dd where jobnumber = ?",
        values: [`${jobnumber}`]
    });
    console.log(count,'===接收计数==CountRecive=')
    ctx.body = count;
    ctx.status = 200
    await next()
});

//紧急流程
router.post('/Imp', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    let data = await BPM.query({
        query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY logintime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Undo_dd where jobnumber = ? and Important=2) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
        values: [jobnumber, page1, page2]
    })
    console.log(data,'===紧急流程==Imp =')
    ctx.body = data;
    ctx.status = 200
    await next()
});

//紧急流程按流程号搜索
router.post('/ImpSearch', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    if (rq.body.FlowID) {
        let data = await BPM.query({
            query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY logintime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Undo_dd where jobnumber = ? and FlowID = ? and Important=2) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
            values: [jobnumber, rq.body.FlowID, page1, page2]
        })
        console.log(data,'===紧急流程搜索==ImpSearch =')
        ctx.body = data;
    } else {
        let data = await BPM.query({
            query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY logintime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Undo_dd where jobnumber = ? and Subject like ? and Important=2) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
            values: [jobnumber, '%' + rq.body.Subject + '%', page1, page2]
        })
        console.log(data,'===紧急流程搜索==ImpSearch =')
        ctx.body = data;
    }
    ctx.status = 200
    await next()
});

//紧急计数
router.post('/CountImp', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let count = await BPM.query({
        query: "select count(*) as c from View_Flow_Undo_dd where jobnumber = ? and Important=2",
        values: [`${jobnumber}`]
    });
    console.log(count,'===紧急计数==CountImp=')
    ctx.body = count;
    ctx.status = 200
    await next()
});

//已办流程
router.post('/Hasdo', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    let data = await BPM.query({
        query: `select * from (select  ROW_NUMBER() OVER ( ORDER BY logintime desc ) AS rowNumber ,* 
            from (select Row_Number() over(Partition By flowid Order by logintime desc) RowID,* 
            from dbo.View_Flow_Hasdo_dd
            where jobnumber= ? )t1 
            where RowID=1 )t2 
        where  rowNumber BETWEEN ? AND ?`,
        values: [jobnumber, page1, page2]
    })
    console.log(data,'===已办流程==hasDo=====hasDo==')
    ctx.body = data;
    ctx.status = 200
    await next()
});

//已办流程搜索
router.post('/HasdoSearch', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    if (rq.body.FlowID) {
        let data = await BPM.query({
            query: `select * from (select  ROW_NUMBER() OVER ( ORDER BY logintime desc ) AS rowNumber ,* 
                from (select Row_Number() over(Partition By flowid Order by logintime desc) RowID,* 
                from dbo.View_Flow_Hasdo_dd
                where jobnumber= ? and  Subject like ?)t1 
                where RowID=1 )t2 
            where  rowNumber BETWEEN ? AND ?`,
            values: [jobnumber, '%' + rq.body.Subject + '%', page1, page2]
        })
        console.log(data,'===已办流程搜索==hasDo=====hasDo==')
        ctx.body = data;
    } else {
        let data = await BPM.query({
            query: `select * from (select  ROW_NUMBER() OVER ( ORDER BY logintime desc ) AS rowNumber ,* 
                from (select Row_Number() over(Partition By flowid Order by logintime desc) RowID,* 
                from dbo.View_Flow_Hasdo_dd
                where jobnumber= ? and  Subject like ?)t1 
                where RowID=1 )t2 
            where  rowNumber BETWEEN ? AND ?`,
            values: [jobnumber, '%' + rq.body.Subject + '%', page1, page2]
        })
        console.log(data,'===已办流程搜索==hasDo=====hasDo==')
        ctx.body = data;
    }
    ctx.status = 200
    await next()
});

//已办计数
router.post('/CountHasdo', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let count = await BPM.query({
        query: `select count(*) as c from (select  ROW_NUMBER() OVER ( ORDER BY logintime desc ) AS rowNumber ,* 
            from (select Row_Number() over(Partition By flowid Order by logintime desc) RowID,* 
            from dbo.View_Flow_Hasdo_dd
            where jobnumber= ? )t1 
        where RowID=1 )t2 `,
        values: [jobnumber]
    });
    console.log(count,'===已办计数==countHasdo=')
    ctx.body = count;
    ctx.status = 200
    await next()
});

//常用流程
router.post('/Fuse', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    let data = await BPM.query({
        query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY userid desc ) AS 'rowNumber' , 
                * FROM dbo.V_FUse_dd where jobnumber = ? or jobnumber = 'sa') AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
        values: [jobnumber, page1, page2]
    })
    console.log(data,'===常用流程==Fuse =')
    ctx.body = data;
    ctx.status = 200
    await next()
});

//常用流程按流程号搜索
router.post('/FuseSearch', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    let data = await BPM.query({
        query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY userid desc ) AS 'rowNumber' , 
                * FROM dbo.V_FUse_dd where (jobnumber= ? or jobnumber = 'sa') and name like ?) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
        values: [jobnumber, '%' + rq.body.name + '%', page1, page2]
    })
    console.log(data,'===常用流程搜索==FuseSearch =')
    ctx.body = data;
    ctx.status = 200
    await next()
});

//常用计数
router.post('/CountFuse', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let count = await BPM.query({
        query: "select count(*) as c from V_FUse_dd where (jobnumber = ? or jobnumber = 'sa')",
        values: [`${jobnumber}`]
    });
    console.log(count,'===常用计数==CountFuse=')
    ctx.body = count;
    ctx.status = 200
    await next()
});

//未读流程
router.post('/UnRead', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    let data = await BPM.query({
        query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY ReceiveTime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Read_dd where loginname = ? and isRead = 0) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
        values: [jobnumber, page1, page2]
    })
    console.log(data,'===未读流程==UnRead =')
    ctx.body = data;
    ctx.status = 200
    await next()
});

//未读计数
router.post('/CountUnRead', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let count = await BPM.query({
        query: "select count(*) as c from View_Flow_Read_dd where loginname = ? and isRead = 0",
        values: [`${jobnumber}`]
    });
    console.log(count,'===未读计数==CountUnRead=')
    ctx.body = count;
    ctx.status = 200
    await next()
});

//未读流程按流程号搜索
router.post('/UnReadSearch', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    if (rq.body.FlowID) {
        let data = await BPM.query({
            query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY ReceiveTime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Read_dd where loginname= ? and isRead = 0 and FlowID = ? ) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
            values: [jobnumber, rq.body.FlowID, page1, page2]
        })
        console.log(data,'===未读流程搜索==UnReadSearch =')
        ctx.body = data;
    } else {
        let data = await BPM.query({
            query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY ReceiveTime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Read_dd where loginname= ? and isRead = 0 and  Subject like ?) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
            values: [jobnumber, '%' + rq.body.Subject + '%', page1, page2]
        })
        console.log(data,'===未读流程搜索==UnReadSearch =')
        ctx.body = data;
    }
    ctx.status = 200
    await next()
});

//办结流程
router.post('/Finishdo', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    let data = await BPM.query({
        query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY StartTime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Finishdo_dd where loginname = ?) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
        values: [jobnumber, page1, page2]
    })
    console.log(data,'===办结流程==Finishdo =')
    ctx.body = data;
    ctx.status = 200
    await next()
});

//办结流程搜索
router.post('/FinishdoSearch', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let rq:any=ctx.request;
    let page1 = (rq.body.page - 1) * 10 + 1
    let page2 = rq.body.page * 10
    if (rq.body.FlowID) {
        let data = await BPM.query({
            query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY StartTime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Finishdo_dd where loginname = ? and FlowID = ? ) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
            values: [jobnumber, rq.body.FlowID, page1, page2]
        })
        console.log(data,'===办结流程搜索==FinishdoSearch =')
        ctx.body = data;
    } else {
        let data = await BPM.query({
            query: `SELECT * FROM
                (SELECT ROW_NUMBER() OVER (ORDER BY StartTime desc ) AS 'rowNumber' , 
                * FROM dbo.View_Flow_Finishdo_dd where loginname = ? and Subject like ?) AS temp
                WHERE rowNumber BETWEEN ? AND ?`,
            values: [jobnumber, '%' + rq.body.Subject + '%', page1, page2]
        })
        console.log(data,'===办结流程搜索==FinishdoSearch =')
        ctx.body = data;
    }
    ctx.status = 200
    await next()
});

//办结计数
router.post('/CountFinishdo', async (ctx: IRouterContext, next: any) => {
    let user =await authService.authUser(ctx.header.authorization);
    let jobnumber = user.jobnumber
    let count = await BPM.query({
        query: "select count(*) as c from View_Flow_Finishdo_dd where loginname = ?",
        values: [`${jobnumber}`]
    });
    console.log(count,'===办结计数==CountFinishdo=')
    ctx.body = count;
    ctx.status = 200
    await next()
});

*/

export default router